package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.pk.domain.PkEjDailyReport;
import com.ruoyi.pk.domain.PkZlDailyReport;
import com.ruoyi.pk.mapper.PkEjDailyReportMapper;
import com.ruoyi.pk.mapper.PkZlDailyReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    @Autowired
    private PkZlDailyReportMapper pkZlDailyReportMapper;
    @Autowired
    private PkEjDailyReportMapper pkEjDailyReportMapper;

    private static final String NAME = "系统自动审核";
    private static final String YJ = "无";
    /**
     * 主粮
     * 品控员提交之后，超过12小时未审核，定时任务自动审核
     * 品控班长->主任->品控经理
     */
    public void zdshZL(){
        System.err.println("开始主粮定时任务");
        PkZlDailyReport zl = new PkZlDailyReport();
        zl.setSftj("1");//1代表已提交，但未审核结束
        List<PkZlDailyReport> pkZlDailyReportList = pkZlDailyReportMapper.selectPkZlDailyReportList(zl);
        pkZlDailyReportList.forEach(item -> {
            //品控班长未审核
            if( get(item.getTxrq(),12) && null==item.getPkbz() || ("").equals(item.getPkbz())){
                PkZlDailyReport zl1 = new PkZlDailyReport();
                zl1.setId(item.getId());
                zl1.setPkbz(NAME);
                zl1.setPkbzyj(YJ);
                zl1.setPkbztxsj(new Date());
                pkZlDailyReportMapper.updatePkZlDailyReport(zl1);
            }
            //主任未审核
            if(item.getPkbztxsj()!=null){
                if(get(item.getPkbztxsj(),12) && null==item.getZr() || ("").equals(item.getZr())){
                    PkZlDailyReport zl1 = new PkZlDailyReport();
                    zl1.setId(item.getId());
                    zl1.setZr(NAME);
                    zl1.setZryj(YJ);
                    zl1.setZrtxsj(new Date());
                    pkZlDailyReportMapper.updatePkZlDailyReport(zl1);
                }
            }
            //品控经理未审核
            if(item.getZrtxsj()!=null) {
                if (get(item.getZrtxsj(), 12) && null == item.getPkjl() || ("").equals(item.getPkjl())) {
                    PkZlDailyReport zl1 = new PkZlDailyReport();
                    zl1.setId(item.getId());
                    zl1.setPkjl(NAME);
                    zl1.setPkjlyj(YJ);
                    zl1.setPkjltxsj(new Date());
                    pkZlDailyReportMapper.updatePkZlDailyReport(zl1);
                }
            }
        });
    }

    /**
     * 二九
     * 品控员提交之后，超过12小时未审核，定时任务自动审核
     * 生产值长->品控主管->品控经理->厂长/主任
     */
    public void ejshZL(){
        System.err.println("开始二九定时任务");
        PkEjDailyReport ej = new PkEjDailyReport();
        ej.setSftj("1");//1代表已提交，但未审核结束
        List<PkEjDailyReport> pkEjDailyReportList = pkEjDailyReportMapper.selectPkEjDailyReportList(ej);
        pkEjDailyReportList.forEach(item -> {
            //值长未审核
            if( get(item.getTxsj(),12) && null==item.getZz() || ("").equals(item.getZz())){
                PkEjDailyReport ej1 = new PkEjDailyReport();
                ej1.setId(item.getId());
                ej1.setZz(NAME);
                ej1.setZzyj(YJ);
                ej1.setZzTxsj(new Date());
                pkEjDailyReportMapper.updatePkEjDailyReport(ej1);
            }
            //品控主管未审核
            if(item.getZzTxsj()!=null){
                if(get(item.getZzTxsj(),12) && null==item.getPkzg() || ("").equals(item.getPkzg())){
                    PkEjDailyReport ej1 = new PkEjDailyReport();
                    ej1.setId(item.getId());
                    ej1.setPkzg(NAME);
                    ej1.setPkzgyj(YJ);
                    ej1.setPkzgTxsj(new Date());
                    pkEjDailyReportMapper.updatePkEjDailyReport(ej1);
                }
            }
            //品控经理未审核
            if(item.getPkzgTxsj()!=null) {
                if (get(item.getPkzgTxsj(), 12) && null == item.getPkjl() || ("").equals(item.getPkjl())) {
                    PkEjDailyReport ej1 = new PkEjDailyReport();
                    ej1.setId(item.getId());
                    ej1.setPkjl(NAME);
                    ej1.setPkjlyj(YJ);
                    ej1.setPkjlTxsj(new Date());
                    pkEjDailyReportMapper.updatePkEjDailyReport(ej1);
                }
            }
            //厂长/主任未审核
            if(item.getPkjlTxsj()!=null) {
                if (get(item.getPkjlTxsj(), 12) && null == item.getCz() || ("").equals(item.getCz())) {
                    PkEjDailyReport ej1 = new PkEjDailyReport();
                    ej1.setId(item.getId());
                    ej1.setCz(NAME);
                    ej1.setCj(YJ);
                    ej1.setCzTxsj(new Date());
                    pkEjDailyReportMapper.updatePkEjDailyReport(ej1);
                }
            }
        });
    }


    /**
     * 两个时间的差
     */
    public static boolean get(Date date,int hour){
        boolean f = false;
        //当前时间
        Date curDate = new Date();
        //求差
        long between = DateUtil.between(date, curDate, DateUnit.SECOND);
        //如果超过指定时间，仍未审核，将执行以下
        if(Long.parseLong(String.valueOf(hour*60*60))<=between){
            System.err.println("超过指定时间未审核");
            f = true;
        }
        return f;
    }
}
